package com.zhang.backend.web.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zhang.backend.common.JsonResponse;
import com.zhang.backend.service.ProductService;
import com.zhang.backend.model.domain.Product;


/**
 * 前端控制器
 *
 * @author zyx
 * @version v1.0
 * @since 2021-12-22
 */
@Controller
@RequestMapping("/product")
public class ProductController {

    private final static Logger logger = LoggerFactory.getLogger(ProductController.class);

    @Autowired
    private ProductService productService;

    /**
     * 描述：根据Id 查询
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id) throws Exception {
        Product product = productService.getById(id);
        return JsonResponse.success(product);
    }

    /**
     * 描述：根据Id删除
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        productService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
     * 描述：根据Id 更新
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateProduct(@PathVariable("id") Long id, Product product) throws Exception {
        product.setId(id);
        productService.updateById(product);
        return JsonResponse.success(null);
    }


    /**
     * 描述:创建Product
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(@RequestBody Product product) throws Exception {
        productService.save(product);
        return JsonResponse.success(null);
    }

    //商品列表
    @GetMapping("/List")
    @ResponseBody
    public JsonResponse productList() {
        return JsonResponse.success(productService.productList());
    }

    //根据名称搜索商品
    @GetMapping("/ByName")
    @ResponseBody
    public JsonResponse productByName(@RequestBody Product product) {
        return JsonResponse.success(productService.selectById(product));
    }
}

